<template>
  <el-dialog
    v-el-drag-dialog
    :title="title"
    :visible.sync="visible"
    width="500px"
    @closed="reset"
  >
    <el-form
      ref="dataFormRef"
      :rules="rules"
      :model="form"
      label-position="right"
      label-width="100px"
    >
      <el-form-item label="检查类别:" prop="itemType">
        <el-select
          v-model="form.itemType"
          placeholder="请选择检查类别"
          style="width: 100%;"
          :disabled="type === 2"
          @change="getItemName"
        >
          <el-option
            v-for="(i, ind) in itemTypeOption"
            :key="ind"
            :label="i.label"
            :value="i.type"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="检查类型:" prop="itemId">
        <el-select
          v-model="form.itemId"
          placeholder="请选择检查类型"
          style="width: 100%;"
          :disabled="type === 2"
        >
          <el-option
            v-for="(i, ind) in itemNameOption"
            :key="ind"
            :label="i.itemName"
            :value="i.itemId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="检查项:" prop="itemDetailName">
        <el-input
          v-model="form.itemDetailName"
          type="textarea"
          :rows="2"
          maxlength="200"
          show-word-limit
          placeholder="请输入检查项名称"
        />
      </el-form-item>
      <el-form-item v-if="showCheck" label="合格判定:" prop="check">
        <el-radio-group v-model="form.check">
          <el-radio :label="1">勾选合格</el-radio>
          <el-radio :label="0">勾选不合格</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="reset"> 关闭 </el-button>
      <el-button :loading="$loadingPlugin.submit" type="primary" @click="save">
        保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { fillingcheckTypeList, fillingcheckSave, fillingcheckUpdate } from '@/api'

  export default {
    name: 'add',
    data() {
      return {
        visible: false,
        showCheck: false, // 是否显示“合格判定”表单项
        itemTypeOption: [
          { itemId: 1, label: '充装前检查', type: 1 },
          { itemId: 2, label: '充装后检查', type: 2 }
        ],
        itemNameOption: [],
        type: 1,
        form: {
          itemId: '',
          itemDetailName: '',
          itemType: '',
          check: 1
        },
        rules: {
          itemId: [{ required: true, message: '请选择检查类别', trigger: 'change' }],
          itemType: [{ required: true, message: '请选择安检类型', trigger: 'change' }],
          itemDetailName: [{ required: true, message: '请输入检查项', trigger: 'change' }],
          check: [{ required: true, message: '请选择合格判断', trigger: 'change' }]
        },
        rowData: {}
      }
    },

    computed: {
      title() {
        return this.type === 1 ? '新增充装检查项' : '修改充装检查项'
      }
    },
    methods: {
      show(type, row) {
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
          this.type = type
          if (type === 2) {
            this.rowData = row
            this.form.itemDetailName = row.itemDetailName
            this.form.check = row.check
            this.showCheck = row.checked
          }
          if (row) {
            this.getItemName(row.itemType, row.itemId)
            this.form = Object.assign(this.form, row)
          }
        })
      },
      save() {
        this.$refs.dataFormRef.validate((valid) => {
          if (!valid) return
          this.submit()
        })
      },
      // 1新增 | 2编辑
      async submit() {
        // const match = this.itemNameOption.find(item => item.itemId === this.form.itemId)
        const params = {
          itemDetailId: this.rowData.itemDetailId || 0,
          // itemDetailName: match ? match.itemName : '',
          itemDetailName: this.form.itemDetailName,
          itemId: this.form.itemId
        }
        if (this.showCheck) params.check = this.form.check
        this.type === 1 ? await fillingcheckSave(params) : await fillingcheckUpdate(params)
        this.resolve()
        this.visible = false
        this.$notify({
          title: '提示',
          message: `${this.type === 1 ? '新增' : '编辑'}成功`,
          type: 'success',
          duration: 2000
        })
      },
      // 检查类型
      async getItemName(val, id) {
        if (val) {
          const { status, data } = await fillingcheckTypeList({
            itemType: val
          })
          if (status === 200) {
            this.itemNameOption = data
          }
        }
      },

      reset() {
        this.$refs['dataFormRef'].resetFields()
        this.visible = false
        this.type = 1
      }
    }
  }
</script>

<style lang="scss" scoped></style>
